from app.lib.model import Model
from tortoise import fields


class Article(Model):
    class Meta:
        table = "Article"

    auto_id = fields.IntField(pk=True)
    share_image_url = fields.CharField(max_length=255, description="")
    topic = fields.CharField(max_length=255, description="主题")
    content = fields.TextField()
    images = fields.JSONField(description="image url list")
    is_recommend = fields.BooleanField(default=False, description="是否被推荐")
    is_down_right = fields.BooleanField(default=False, description="是否被降权")
    videos = fields.JSONField(description="video url 列表")
    cover_image = fields.CharField(max_length=255, description="封面")
    region = fields.CharField(max_length=255, description="地区")
    updated_at = fields.DatetimeField(auto_now=True)
    created_at = fields.DatetimeField(auto_now_add=True)

    user = fields.ForeignKeyField("models.User", related_name="articles")
    likes = fields.ManyToManyField(
        "models.User",
        through="UserLikeArticle",
        related_name="liked_articles",
        forward_key="user_id",
        backward_key="article_id",
    )

    comments: fields.ForeignKeyRelation["Comment"]  # noqa: F821

    async def get_like_count(self):
        return self.likes.all().count()
